﻿using System.IO;
using GameFramework;
using OfficeOpenXml;
using UnityEditor;
using Debug = UnityEngine.Debug;

namespace DTFramework.Editor.DataTable
{
    public sealed class DataTableGeneratorMenu
    {
        //[MenuItem("DTFramework/DataTable/Generate From Txt", priority = 1)]
        //public static void GenerateDataTablesFromTxtNotFileSystem()
        //{
        //    DataTableConfig.GetDataTableConfig().RefreshDataTables();

        //    ExtensionsGenerate.GenerateExtensionByAnalysis(ExtensionsGenerate.DataTableType.Txt,
        //        DataTableConfig.GetDataTableConfig().TxtFilePaths, 2);
        //    foreach (var dataTableName in DataTableConfig.GetDataTableConfig().DataTableNames)
        //    {
        //        var dataTableProcessor = DataTableGenerator.CreateDataTableProcessor(dataTableName);
        //        if (!DataTableGenerator.CheckRawData(dataTableProcessor, dataTableName))
        //        {
        //            Debug.LogError(Utility.Text.Format("Check raw data failure. DataTableName='{0}'", dataTableName));
        //            break;
        //        }

        //        DataTableGenerator.GenerateDataFile(dataTableProcessor, dataTableName);
        //        DataTableGenerator.GenerateCodeFile(dataTableProcessor, dataTableName);
        //    }

        //    AssetDatabase.Refresh();
        //}

        [MenuItem("DTFramework/DataTable/Generate .byte From Excel", priority = 2)]
        public static void GenerateDataTablesFormExcelNotFileSystem()
        {
            DataTableConfig.GetDataTableConfig().RefreshDataTables();

            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
            ExtensionsGenerate.GenerateExtensionByAnalysis(ExtensionsGenerate.DataTableType.Excel,
                DataTableConfig.GetDataTableConfig().ExcelFilePaths, 2);
            foreach (var excelFile in DataTableConfig.GetDataTableConfig().ExcelFilePaths)
            {
                using (FileStream fileStream =
                       new FileStream(excelFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    using (ExcelPackage excelPackage = new ExcelPackage(fileStream))
                    {
                        for (int i = 0; i < excelPackage.Workbook.Worksheets.Count; i++)
                        {
                            ExcelWorksheet sheet = excelPackage.Workbook.Worksheets[i];
                            var dataTableProcessor = DataTableGenerator.CreateExcelDataTableProcessor(sheet);
                            if (!DataTableGenerator.CheckRawData(dataTableProcessor, sheet.Name))
                            {
                                Debug.LogError(Utility.Text.Format("Check raw data failure. DataTableName='{0}'",
                                    sheet.Name));
                                break;
                            }

                            DataTableGenerator.GenerateDataFile(dataTableProcessor, sheet.Name);
                            DataTableGenerator.GenerateCodeFile(dataTableProcessor, sheet.Name);
                        }
                    }
                }
            }

            AssetDatabase.Refresh();
        }
        [MenuItem("DTFramework/DataTable/Generate .txt From Excel", priority = 3)]
        public static void ExcelToTxt()
        {
            DataTableConfig.GetDataTableConfig().RefreshDataTables();
            if (!Directory.Exists(DataTableConfig.GetDataTableConfig().DataTableFolderPath))
            {
                Debug.LogError($"{DataTableConfig.GetDataTableConfig().DataTableFolderPath} is not exist!");
                return;
            }

            ExcelExtension.ExcelToTxt(DataTableConfig.GetDataTableConfig().DataTableFolderPath,
                DataTableConfig.GetDataTableConfig().DataTableFolderPath);
            AssetDatabase.Refresh();
        }

        [MenuItem("DTFramework/DataTable/Create DataTable Config", priority = 21)]
        public static void CreateDataTableConfig()
        {
            DataTableConfig.CreateDataTableConfig();
        }
    }
}